package com.jiang.servlet;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(name = "reverseAjax", urlPatterns = "/reverseAjaxServlet/*", asyncSupported=true)
public class ReverseAjaxServlet extends HttpServlet {

	private static final long serialVersionUID = 6476697304887302324L;

	// 内部类用来反向推送的
	class MyMessage implements Runnable{

		private AsyncContext asy;
		
		/**
		 * @构造函数 传进来数据
		 * @param asy
		 */
		public MyMessage(AsyncContext asy){
			this.asy = asy;
		}
		
		@Override
		public void run() {
			// 使用AsyncContext来获取request对象
			ServletRequest request =  asy.getRequest();
			String msg = request.getParameter("msg");
			// 进行普通消息输出
			try {
				asy.getResponse().getWriter().println("Echo:" + msg);
				// 输出完毕要关闭
				asy.complete();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 这里主要是靠路径来区分调用的业务方法
		String uri = request.getRequestURI();
		String methodName = uri.substring(uri.lastIndexOf("/") + 1);
		try {
			Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,
					HttpServletResponse.class);
			// 直接调用其方法，返回其结果
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

	public void getInfo(HttpServletRequest request, HttpServletResponse response) {
			// 创建这个类，然后启动线程
		AsyncContext asy = request.startAsync();
		MyMessage myMessage = new MyMessage(asy);
		// 然后由该对象启动线程
		asy.start(myMessage);
	}

}
